package com.Touch.TouchSimulator
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.ServerSocketConnectEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.net.ServerSocket;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	
	public class MultTouchSupport
	{
		
		private var serversocket:ServerSocket;
		
		private var socket_list:Vector.<Socket>;
		
		private var touch_list:Vector.<SimulateTouchAppliedObj>;
		
		private var stage:Stage;
		
		private var stageWidth:int;
		
		private var stageHeight:int;
		
		public function MultTouchSupport(){
			serversocket=new ServerSocket();
			touch_list=new Vector.<SimulateTouchAppliedObj>();
			socket_list=new Vector.<Socket>();
		}
		
		/**
		 * 初始化
		 */
		public  function init(_stage:Stage,_ip:String="127.0.0.1"):void{
			stage=_stage;
			stageWidth=stage.width;
			stageHeight=stage.height;
			try{
				if(serversocket.listening){
					serversocket.close();
					touch_list.splice(0,touch_list.length);
					socket_list.splice(0,socket_list.length);
				}
				serversocket.bind(763,_ip);
				serversocket.addEventListener(ServerSocketConnectEvent.CONNECT,on_connect);
				serversocket.listen();
			}catch(error:Error){
				//提示错误信息
			}
		}
		
		//链接打开则连接该点
		private function on_connect(event:ServerSocketConnectEvent):void{
			var temp_socket:Socket = event.socket;
			temp_socket.addEventListener(Event.CLOSE,close_connect);
			temp_socket.addEventListener(ProgressEvent.SOCKET_DATA,on_socket_data);
			
			socket_list.push(temp_socket);
		}
		
		//链接关闭则移除该链接
		private function close_connect(event:Event):void{
			for (var i:int=0;i<socket_list.length;i++){
				if(socket_list[i].remoteAddress==event.target.remoteAddress){
					socket_list.splice(i,1);
				}
			}
		}
		
		//数据到达
		private  function on_socket_data(event:ProgressEvent):void{
			var message:ByteArray=new ByteArray();
			event.target.readBytes(message,0,event.target.bytesAvailable );
			try{
				//格式化xml 并检查xml格式
				message.uncompress();
				var xml:XML=XML(message.toString());
				
				
				if(xml.dt!="dt"){
					return;
				}
			}catch(error:Error){
				return;
			}
				
			var local_ip:String=event.target.remoteAddress;
			
			//建立循环
			var listLength:int=xml.obj.length();
			for(var i:int=0;i<listLength;i++){
				var local_id:int=int(xml.obj[i].id);
				var state:String=xml.obj[i].state;
				var x:int=Number(xml.obj[i].x)*stageWidth;
				var y:int=Number(xml.obj[i].y)*stageHeight;
				
				//获取显示对象列表  得到最上层对象  如果没有则返回空值
				var disobj:DisplayObject=null;
				var objArray:Array = stage.getObjectsUnderPoint(new Point(x, y));
				if(objArray.length>0){
					disobj=objArray[objArray.length-1];
					if(disobj is Bitmap){
						disobj=disobj.parent;
					}
				}
				
				//查找实例
				var tempobj:SimulateTouchAppliedObj=isexist(local_id,local_ip);
				
				//如果没有这个点则新建一个点
				if(tempobj==null){
					//取得没有被占用的触摸点值
					var id:int=CreatePoint();
					if(id>0){
						tempobj=new SimulateTouchAppliedObj(id,stage,local_ip,local_id);
						tempobj.TouchOn(x,y,disobj);
						touch_list.push(tempobj);
					}else{
						return;//超过点数上限
					}
				}else{
					if(state=="true"){
						//点存在
						tempobj.TouchMove(x,y,disobj);
						
					}else{
						tempobj.TouchEnd(x,y,disobj);
						//删除点
						for(var k:int=0;k<touch_list.length;k++){
							if(tempobj.id==touch_list[k].id){
								touch_list.splice(k,1);
							}
						}
					}
				}
			}
		}
		
		//检查是否存在点
		private function isexist(_id:int,_ip:String):SimulateTouchAppliedObj{
			for each(var temp:SimulateTouchAppliedObj in touch_list){
				if(temp.local_id==_id){
					if(temp.local_ip==_ip){
						return temp;
					}
				}
			}
			return null;
		}
		
		//创建一个新点
		private function CreatePoint():int{
			for(var i:int=128;i<192;i++){
				var has:Boolean=false;//判断此序列是否在列表中存在
				for each(var temp:SimulateTouchAppliedObj in touch_list){
					if(i==temp.id){
						has=true;
					}
				}
				if(!has){//如果不存在则返回
					return i;
				}
			}
			return -1;//超过上限则取消
		}
	}
}